// pages/resultype/dioptertest/dioptertest.js
const commonutil = require("../commonutil.js");
const screenresultservice = require("../../../apiservice/screen/screenresultservice.js");
const app = getApp();
const regeneratorRuntime = app.globalData.regeneratorRuntime;
Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		scid: '',
		customerid: '',
		optionShow: false,
		addOrMod: "add", // 控制提交和修改按钮的显示
    schoolId:'',
		/**屈光度数检查**/
		//球面度数-右眼
		sphericalDegreeRight: {
			new_scid: null,
			new_categoryid: 'LB0013',
			new_itemid: 'XM0047',
			new_contentid: 'NR0143',
			new_result: '',
			new_doctorid: null,
			ownerid: null
		},

		//球面度数-左眼
		sphericalDegreeleft: {
			new_scid: null,
			new_categoryid: 'LB0013',
			new_itemid: 'XM0047',
			new_contentid: 'NR0144',
			new_result: '',
			new_doctorid: null,
			ownerid: null
		},

		//散光度数-右眼
		cylRight: {
			new_scid: null,
			new_categoryid: 'LB0013',
			new_itemid: 'XM0049',
			new_contentid: 'NR0145',
			new_result: '',
			new_doctorid: null,
			ownerid: null
		},

		//散光度数-左眼
		cylLeft: {
			new_scid: null,
			new_categoryid: 'LB0013',
			new_itemid: 'XM0049',
			new_contentid: 'NR0146',
			new_result: '',
			new_doctorid: null,
			ownerid: null
		},

		//散光轴度-右眼
		astigmatismAxisRight: {
			new_scid: null,
			new_categoryid: 'LB0013',
			new_itemid: 'XM0050',
			new_contentid: 'NR0147',
			new_result: '',
			new_doctorid: null,
			ownerid: null
		},
		//散光轴度-左眼
		astigmatismAxisLeft: {
			new_scid: null,
			new_categoryid: 'LB0013',
			new_itemid: 'XM0050',
			new_contentid: 'NR0148',
			new_result: '',
			new_doctorid: null,
			ownerid: null
		},
		//瞳孔大小-右眼
		pupilSizeRight: {
			new_scid: null,
			new_categoryid: 'LB0013',
			new_itemid: 'XM0051',
			new_contentid: 'NR0149',
			new_result: '',
			new_doctorid: null,
			ownerid: null
		},
		//瞳孔大小-左眼
		pupilSizeLeft: {
			new_scid: null,
			new_categoryid: 'LB0013',
			new_itemid: 'XM0051',
			new_contentid: 'NR0150',
			new_result: '',
			new_doctorid: null,
			ownerid: null
		},
		//瞳距
		pupilDistance: {
			new_scid: null,
			new_categoryid: 'LB0013',
			new_itemid: 'XM0052',
			new_contentid: 'NR0151',
			new_result: '',
			new_doctorid: null,
			ownerid: null
		},
		//凝视不对成度
		gazeMisalignment: {
			new_scid: null,
			new_categoryid: 'LB0013',
			new_itemid: 'XM0053',
			new_contentid: 'NR0152',
			new_result: '',
			new_doctorid: null,
			ownerid: null
		},
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		var options = options;
		if (options) {
			this.setData({
				leadName: options.leadname,
				customerid: options.sccustid,
				scid: options.scid,
				categoryid: options.categoryid
			})
			this.getDioptertestResults(options.scid, options.sccustid, options.categoryid);
      if(options.schoolId){
        this.setData({schoolId:options.schoolId});
      }
		}
	},
	//提交表单
	dioptertestSubmit: function(e) {
		var screenResultRequest = {}
		var createResult = this.getDioptertest();
		screenResultRequest.screenResults = createResult;
		screenResultRequest.screenCustomer = {
			new_customerid:this.data.customerid,
			new_scid:this.data.scid
		}
		var that = this;
		commonutil.createScreenResultsSchool(screenResultRequest, function(res) {
			var idArray = res.result.split('|');
			for (var i = 0; i < idArray.length; i++) {
				createResult[i].new_sc_resultId = idArray[i];
			}
			that.setData({
				addOrMod: "mod",
				sphericalDegreeRight: createResult[0],
				sphericalDegreeleft: createResult[1],
				cylRight: createResult[2],
				cylLeft: createResult[3],
				astigmatismAxisRight: createResult[4],
				astigmatismAxisLeft: createResult[5],
				pupilSizeRight: createResult[6],
				pupilSizeLeft: createResult[7],
				pupilDistance: createResult[8],
				gazeMisalignment: createResult[9],
			});
		});
	},
	//白内障检查更新
	dioptertestUpdate: function() {
		var updateResult = this.getDioptertest();
		commonutil.updateScreenResults(updateResult);
	},
	//查看报告
	enterDetail: async function(e) {
		var scid = this.data.scid;
		var leadid = this.data.customerid;
		var lead = {};
		var data = await commonutil.getLeadInfo(leadid);
		lead.leadid = data.leadId;
		lead.customername = data.lastName;
		lead.age = data.new_age;
		lead.sex = data.new_gender;
		lead.iphoneno1 = data.new_phone;
		lead.sfz = data.new_citizenid;
		lead.createdon = data.modifiedOn;
    var url = "/pages/print/letter/letter?scid=" + scid;
    if (this.data.schoolId)
      url += "&schoolId=" + this.data.schoolId;
		wx.navigateTo({
			url: url,
			events: {
				// 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
				acceptDataFromOpenedPage: function(data) {
					console.log(data)
				},
				someEvent: function(data) {
					console.log(data)
				}
			},
			success: function(res) {
				// 通过eventChannel向被打开页面传送数据
				res.eventChannel.emit('acceptDataFromOpenerPage', {
					data: lead
				})
			}
		})
	},
	//获取结果集
	getDioptertestResults: async function(scid, customerid, categoryid) {
		wx.showLoading({
			title: '加载中...',
			mask: true,
		})
		var success = await screenresultservice.GetScreenResultByCategory(scid, customerid, categoryid);
		var reusltArray = success.data
		if (reusltArray.length == 0) {
			wx.hideLoading();
			return;
		}
		this.setData({
			addOrMod: 'mod'
		})
		console.log(reusltArray);
		for (let index in reusltArray) {
			var element = reusltArray[index];
			if (element.new_contentid === 'NR0143') {
				this.setData({
					sphericalDegreeRight: element
				})
			} else if (element.new_contentid === 'NR0144') {
				this.setData({
					sphericalDegreeleft: element
				})
			} else if (element.new_contentid === 'NR0145') {
				this.setData({
					cylRight: element
				})
			} else if (element.new_contentid === 'NR0146') {
				this.setData({
					cylLeft: element
				})
			} else if (element.new_contentid === 'NR0147') {
				this.setData({
					astigmatismAxisRight: element
				})
			} else if (element.new_contentid === 'NR0148') {
				this.setData({
					astigmatismAxisLeft: element
				})
			} else if (element.new_contentid === 'NR0149') {
				this.setData({
					pupilSizeRight: element
				})
			} else if (element.new_contentid === 'NR0150') {
				this.setData({
					pupilSizeLeft: element
				})
			} else if (element.new_contentid === 'NR0151') {
				this.setData({
					pupilDistance: element
				})
			} else if (element.new_contentid === 'NR0152') {
				this.setData({
					gazeMisalignment: element
				})
			}
		}
		wx.hideLoading();
	},
	/**选项集、文本赋值**/
	onSphericalDegreeleftChange: function(e) {
		var result = 'sphericalDegreeleft.new_result';
		this.setData({
			[result]: e.detail,
		})
	},
	onSphericalDegreeRightChange: function(e) {
		var result = 'sphericalDegreeRight.new_result';
		this.setData({
			[result]: e.detail,
		})
	},
	onCylRightChange: function(e) {
		var result = 'cylRight.new_result';
		this.setData({
			[result]: e.detail,
		})
	},
	onCylLeftChange: function(e) {
		var result = 'cylLeft.new_result';
		this.setData({
			[result]: e.detail,
		})
	},
	onAstigmatismAxisRightChange: function(e) {
		var result = 'astigmatismAxisRight.new_result';
		this.setData({
			[result]: e.detail,
		})
	},
	onAstigmatismAxisLeftChange: function(e) {
		var result = 'astigmatismAxisLeft.new_result';
		this.setData({
			[result]: e.detail,
		})
	},
	onPupilSizeRightChange: function(e) {
		var result = 'pupilSizeRight.new_result';
		this.setData({
			[result]: e.detail,
		})
	},
	onPupilSizeLeftChange: function(e) {
		var result = 'pupilSizeLeft.new_result';
		this.setData({
			[result]: e.detail,
		})
	},
	onPupilDistanceChange: function(e) {
		var result = 'pupilDistance.new_result';
		this.setData({
			[result]: e.detail,
		})
	},
	onGazeMisalignmentChange: function(e) {
		var result = 'gazeMisalignment.new_result';
		this.setData({
			[result]: e.detail,
		})
	},
	/**获取createResult**/
	getDioptertest: function() {
		var createResult = [];
		var scid = this.data.scid;
		var customerid = this.data.customerid;

		var sphericalDegreeRight = this.data.sphericalDegreeRight;
		var sphericalDegreeleft = this.data.sphericalDegreeleft;
		var cylRight = this.data.cylRight;
		var cylLeft = this.data.cylLeft;
		var astigmatismAxisRight = this.data.astigmatismAxisRight;
		var astigmatismAxisLeft = this.data.astigmatismAxisLeft;
		var pupilSizeRight = this.data.pupilSizeRight;
		var pupilSizeLeft = this.data.pupilSizeLeft;
		var pupilDistance = this.data.pupilDistance;
		var gazeMisalignment = this.data.gazeMisalignment;

		if(sphericalDegreeRight.new_result!=''){
			createResult.push(sphericalDegreeRight);
		}	
		if(sphericalDegreeleft.new_result!=''){
			createResult.push(sphericalDegreeleft);
		}	
		if(cylRight.new_result!=''){
			createResult.push(cylRight);
		}
		if(cylLeft.new_result!=''){
			createResult.push(cylLeft);
		}
		if(astigmatismAxisRight.new_result!=''){
			createResult.push(astigmatismAxisRight);
		}
		if(astigmatismAxisLeft.new_result!=''){
			createResult.push(astigmatismAxisLeft);	
		}
		if(pupilSizeRight.new_result!=''){
			createResult.push(pupilSizeRight);
		}		
		if(pupilSizeLeft.new_result!=''){
			createResult.push(pupilSizeLeft);
		}		
		if(pupilDistance.new_result!=''){
			createResult.push(pupilDistance);
		}	
		if(gazeMisalignment.new_result!=''){
			createResult.push(gazeMisalignment);
		}
		//给各结果的筛查id/联系人id赋值
		createResult.forEach(element => {
			element.new_scid = scid;
			element.new_customerid = customerid;
		});
		return createResult;
	}

})
